package com.mlr.mlrcturebackend.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mlr.mlrcturebackend.exception.BusinessException;
import com.mlr.mlrcturebackend.exception.ErrorCode;
import com.mlr.mlrcturebackend.exception.ThrowUtils;
import com.mlr.mlrcturebackend.model.dto.tag.TagQueryRequest;
import com.mlr.mlrcturebackend.model.entity.Tag;
import com.mlr.mlrcturebackend.model.vo.TagVO;
import com.mlr.mlrcturebackend.service.TagService;
import com.mlr.mlrcturebackend.mapper.TagMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mlr
 * @description 针对表【tag(标签表)】的数据库操作Service实现
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
        implements TagService {

    @Override
    public void validTag(Tag tag, boolean add) {
        ThrowUtils.throwIf(tag == null, ErrorCode.PARAMS_ERROR);
        String tagName = tag.getTagName();

        // 创建时校验
        if (add) {
            ThrowUtils.throwIf(StrUtil.isBlank(tagName), ErrorCode.PARAMS_ERROR, "标签名称不能为空");
        }

        // 校验长度
        ThrowUtils.throwIf(StrUtil.isNotBlank(tagName) && tagName.length() > 64,
                ErrorCode.PARAMS_ERROR, "标签名称过长");
        ThrowUtils.throwIf(StrUtil.isNotBlank(tag.getDescription()) && tag.getDescription().length() > 255,
                ErrorCode.PARAMS_ERROR, "标签描述过长");
    }

    @Override
    public QueryWrapper<Tag> getQueryWrapper(TagQueryRequest tagQueryRequest) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        if (tagQueryRequest == null) {
            return queryWrapper;
        }

        String tagName = tagQueryRequest.getTagName();
        queryWrapper.like(StrUtil.isNotBlank(tagName), "tagName", tagName);
        queryWrapper.eq("isDelete", 0);
        queryWrapper.orderByDesc("updateTime");

        return queryWrapper;
    }

    @Override
    public Tag getOrCreateTagByName(String tagName) {
        if (StrUtil.isBlank(tagName)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签名称不能为空");
        }

        Tag tag = this.getOne(new QueryWrapper<Tag>().eq("tagName", tagName).eq("isDelete", 0));
        if (tag == null) {
            tag = new Tag();
            tag.setTagName(tagName);
            this.save(tag);
        }
        return tag;
    }

    @Override
    public List<Tag> batchGetOrCreateTags(List<String> tagNames) {
        if (tagNames == null || tagNames.isEmpty()) {
            return new ArrayList<>();
        }

        List<Tag> resultList = new ArrayList<>();
        for (String tagName : tagNames) {
            resultList.add(getOrCreateTagByName(tagName));
        }
        return resultList;
    }

    @Override
    public TagVO getTagVO(Tag tag) {
        if (tag == null) {
            return null;
        }
        return TagVO.objToVo(tag);
    }

    @Override
    public List<TagVO> getTagVOList(List<Tag> tagList) {
        if (tagList == null || tagList.isEmpty()) {
            return new ArrayList<>();
        }
        return tagList.stream().map(this::getTagVO).collect(Collectors.toList());
    }

}